// pages/toocle/shop/search-result/search-result.js
import http from '../../../../class/Utils/Http.js'
import tips from '../../../../class/Utils/Tips.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag: 0,
        list: [],
        cid: 0,
        time: 0,
        page: 1,
        pname: '',
        nodata: 0,
        fenye: true
    },
    //搜索跳转页面
    searchlink: function (e) {
        var that = this
        wx.redirectTo({
          url: '/pages/toocle/shop/search/search?cid=' + that.data.cid + '&time=' + that.data.time + '&menus=' + that.data.menus + "&mid=" + that.data.mid
        })
    },
    //点击切换形式
    changearray: function (e) {
        var that = this;
        var menuflagno = that.data.flag;
        that.setData({
            flag: !menuflagno,
        })
    },

    onLoad: function (p) {
      var that = this
      var param = {}
      param.cid = p.cid
      param.time = p.time
      param.terms = p.terms

      var menus = p.menus
      if (menus != undefined) {
        that.setData({
          menus: menus
        })
      }

      var mid = p.mid
      if (mid) {
        that.setData({
          mid: mid
        })
      }


      that.setData(param)
      http.reqload('/company/products', param, function (res) {

        //开始判断 有没有 http begin
        var newlistproducts = []
        for (var i = 0; i < res.list_products.length; i++) {
          if (res.list_products[i].pic1 && !/http/.test(res.list_products[i].pic1)) {
            res.list_products[i].pic1 = "http://img.i.album.toocle.com/230-230/" + res.list_products[i].pic1
          }
          newlistproducts.push(res.list_products[i])

        }

      //开始判断 有没有 http end
            
            that.setData({
                info: res,
              list: newlistproducts
            })
            if (res.list_products.length < 1) {
              that.setData({
                nodata: 1
              })
            }
        });

    },
    onReachBottom: function () {
        var that = this

      if (that.data.fenye) {

        var page = that.data.page
        page++
        that.setData({
            page: page
        })
       

        var data = {
            terms: that.data.terms,
            cid: that.data.cid,
            time: that.data.time,
            page: page
        }
        var list = that.data.list
        http.reqload("/company/products", data, function (res) {
           
            if (res.list_products.length > 0) {

                for (var i = 0; i < res.list_products.length; i++) {
                  if (res.list_products[i].pic1 && !/http/.test(res.list_products[i].pic1)) {
                    res.list_products[i].pic1 = "http://img.i.album.toocle.com/230-230/" + res.list_products[i].pic1
                  }
                    list.push(res.list_products[i])
                }
                that.setData({
                    list: list
                })
            }else {
                that.setData({
                  fenye: false
                })
                tips.alert_none('没有数据了')
            }
        })
      }
    }


})